proc script_run {} {
        global info sample_expect sample_cmd scriptfile_name cmdfile_name
        set w .script_tools
        catch {destroy $w}
        toplevel $w
        wm title $w "Create/Run script on device"
        wm iconname $w "iconname?"
        positionWindow $w

        frame $w.left 
        frame $w.left.label
        frame $w.left.edit
        frame $w.left.cmd
        frame $w.right 
        frame $w.right.label
        frame $w.right.edit
        frame $w.right.cmd
        pack $w.left $w.right -side left  -padx 3 -pady 3 -expand 1 -fill both 
        pack $w.left.label -side top -padx 5 -pady 5 -fill x
        pack $w.left.edit -side top -padx 5 -pady 5 -expand 1 -fill both
        pack $w.left.cmd -side top -padx 5 -pady 5  -fill x
        pack $w.right.label -side top  -padx 5 -pady 5 -fill x
        pack $w.right.edit -side top  -padx 5 -pady 5 -expand 1 -fill both
        pack $w.right.cmd -side top  -padx 5 -pady 5 -fill x
        label $w.left.label.label -text "Edit Expect or Shell script"
        pack $w.left.label.label -side top -expand 1 -fill both
        set script_txt [text $w.left.edit.txt -yscrollcommand "$w.left.edit.sb set" -height 25 -width 50]
        scrollbar $w.left.edit.sb -orient vertical -command "$script_txt yview"
        pack $script_txt -side left -expand 1 -fill both
        pack $w.left.edit.sb -side left  -fill y
        $script_txt insert end $sample_expect
        label $w.right.label.label -text "Created/loaded cmd text"
        pack $w.right.label.label -side top -expand 1 -fill both
        set cmd_txt [text $w.right.edit.txt -yscrollcommand "$w.right.edit.sb set" -height 25 -width 50]
        scrollbar $w.right.edit.sb -orient vertical -command "$cmd_txt yview"
        pack $cmd_txt -side left -expand 1 -fill both
        pack $w.right.edit.sb -side left -fill y
        $cmd_txt insert end $sample_cmd
	set tcltype ".tcl"
	set cmdtype ".cmd"
	set scriptfile_name ""
	set cmdfile_name ""

        button $w.left.cmd.run_as_expect -text "Run as expect" -command "run_expect_script $script_txt $cmd_txt;  "
        button $w.left.cmd.run_as_shell -text "Run as shell" -command "run_shell_script $script_txt $cmd_txt;  "
        button $w.left.cmd.save -text "Save" -command "save_file $script_txt $tcltype scriptfile_name;  "
        button $w.left.cmd.load -text "Load" -command "load_file $script_txt scriptfile_name;  "
        button $w.left.cmd.exit -text "Exit" -command " catch {destroy $w} ; return 0 "
        pack $w.left.cmd.run_as_expect $w.left.cmd.run_as_shell $w.left.cmd.save $w.left.cmd.load $w.left.cmd.exit -side left -expand 1 -fill both

	set entry_widget [entry $w.right.cmd.dev_list  -relief sunken ]
        button $w.right.cmd.run_cmd_on_dev -text "Run on device:" -command "run_on_device $entry_widget $cmd_txt; "
        button $w.right.cmd.sele -text "Select device" -command "get_device $entry_widget; "
        button $w.right.cmd.save -text "Save" -state normal -command "save_file $cmd_txt $cmdtype cmdfile_name; "
        button $w.right.cmd.load -text "Load" -state normal -command "load_file $cmd_txt cmdfile_name; "
        pack $w.right.cmd.run_cmd_on_dev $w.right.cmd.dev_list $w.right.cmd.sele $w.right.cmd.save $w.right.cmd.load -side left -expand 1 -fill both

}
proc get_list {sum_line dev_array entry_widget} {
	global info
		log_info $info "dev_array: $dev_array " 2
	upvar $dev_array array1
#	uplevel 1 array1
#	array set array1 $dev_array
	set list_value ""
        for {set i 1} {$i <= $sum_line} {incr i +1} {
		log_info $info "array1($i,1): $array1($i,1);array1($i,2): $array1($i,2) " 2
                if {$array1($i,2)==1} {
			append list_value $array1($i,1)
			append list_value ";"
                } 
        }
	log_info $info "before return, list_value is $list_value" 2
	$entry_widget delete 0 end
	$entry_widget insert 0 $list_value
}
proc get_device {entry_widget} {
        global info DEV_TB db_handle array1 

        set w_dev .add_new_env
        catch {destroy $w_dev}
        toplevel $w_dev
        wm title $w_dev "Select devices "
        wm iconname $w_dev "iconname?"
        positionWindow $w_dev
	set dev_list ""
#create frame window man_win for device list
        set dev_list [ScrolledWindow $w_dev.dev -relief sunken -borderwidth 2]
        set sdev_list [ScrollableFrame $w_dev.dev.f -height 500 -width 220 -constrainedwidth 1 -bg white]
        $dev_list setwidget $sdev_list
        set w [$sdev_list getframe]
        pack $dev_list -side top
# get device list from dev_access table
        set sql_qur "SELECT name from $DEV_TB ORDER BY name"
        set sky_response [mysql_pack $sql_qur $DB_NAME ]
        set parent_line [lindex $sky_response 0]
        set sql_ret_str [lindex $sky_response 1]
        set k 0

        if {$parent_line > "0"} {
                log_info $info "Found $parent_line device records in DB!\n" 5
                for {set i 1} {$i <= $parent_line} {incr i +1} { 
                        set array1($i,1) [lindex $sql_ret_str $k]
                        incr k +1
                        set array1($i,2) 0
                        log_info $info "array1($i,1): $array1($i,1); array1($i,2): $array1($i,2)" 2
                        checkbutton $w.$i -text $array1($i,1) -variable array1($i,2) -anchor w
                        pack $w.$i -side top -pady 2 -anchor w
                }
        } else {
                log_info $info "Cannot find any device records in DB!\n You need add device firstly" 2
                return 0
        }
	set abc [array get array1]
        frame $w_dev.command
        button $w_dev.command.confirm -text Confirm -command "catch {destroy $w_dev}; get_list $parent_line array1 $entry_widget " 
        button $w_dev.command.cancel -text Cancel -command " catch {destroy $w_dev} ; "
        pack $w_dev.command -side bottom  -fill x -pady 2m
        pack $w_dev.command.confirm $w_dev.command.cancel -side left -expand 1
}

proc run_on_device {dev_list_obj txt_cmd_obj} {
	global info interrupt
        #reset interrupt
	set interrupt 0
	#Run cmd group which is in txt_cmd_obj on device
	set run_cmd_grp [$txt_cmd_obj get 1.0 end]
#	set device_list [split [get_device] ";"]
	set dev_list [$dev_list_obj get]
	set dev_list [string trim [string trim $dev_list] ";"]
	append dev_list ";"
	set device_list [split $dev_list ";"]
	log_info $info "device_list is $device_list" 2
	for {set i 0} {$i<[expr [llength $device_list]-1]} {incr i +1} {
		set run_dev [lindex $device_list $i]
		if {[set run_result [run_comm_on_dev $run_dev $run_cmd_grp 1]]==0} {
			log_info $info "failed to run cmd on dev:$run_dev" 2
		} else { 
			log_info $info "succeed to run cmd on dev:$run_dev" 2
		}

	}
}

proc run_expect_script {run_txt run_result} {
	#Using expect to run content in text object "run_txt", insert result to text object "run_result"
        global info file_for_tmp_exp_script file_for_tmp_exp_result SCRIPT_DIR
        set new_content [$run_txt get 1.0 end]
#        set new_content [string map {\" \\" \' \\'} $new_content]
	if [catch {set filehandle [open data/script_header.txt r]} err_info] {
		log_info $info "Error to open file data/script_header.txt. Reason: $err_info" 2
		return 0
	}
set header_content [read $filehandle]
        if [catch {close $filehandle} err_info] {
                log_info $info "Error to close file. Reason: $err_info" 2
                return 0
        }

        if [catch {set filehandle [open $file_for_tmp_exp_script w]} err_info] {
                log_info $info "Error to open file $file_for_tmp_exp_script. Reason: $err_info" 2
                return 0
        }

	log_info $info "content is $new_content" 2
	log_info $info "filename is $file_for_tmp_exp_script ; filehandle is $filehandle" 2
_puts $filehandle "$header_content"
	_puts $filehandle "$new_content"
        if [catch {close $filehandle} err_info] {
                log_info $info "Error to close file. Reason: $err_info" 2
		return 0
        }
eval_exec_cmd "chmod 755 $file_for_tmp_exp_script"
eval cd $SCRIPT_DIR
eval pwd
        if [catch { eval exec $file_for_tmp_exp_script > $file_for_tmp_exp_result} err_info] {
                log_info $info "Error to execute tclsh . Reason: $err_info" 2
                return 0
        }

        if [catch { eval exec $file_for_tmp_exp_script > $file_for_tmp_exp_result} err_info] {
                log_info $info "Error to execute tclsh . Reason: $err_info" 2
                return 0
        }
        if [catch {set filehandle [open $file_for_tmp_exp_result]} err_info] {
                log_info $info "Error to open file $file_for_tmp_exp_result . Reason: $err_info" 2
                return 0
        }
        set result [read $filehandle]
        log_info $info "result is $result" 2
        log_info $info "filehandle is $filehandle" 2
        if [catch {close $filehandle} err_info] {
                log_info $info "Error to close file. Reason: $err_info" 2
                return 0
        }
	$run_result delete 1.0 end
        $run_result insert end $result
}

proc run_shell_script {run_txt run_result} {
        #Using /bin/bash to run content in text object "run_txt", insert result to text object "run_result"
        global info file_for_tmp_sh_script file_for_tmp_sh_result
        set new_content [$run_txt get 1.0 end]
#        set new_content [string map {\" \\" \' \\'} $new_content]
        if [catch {set filehandle [open $file_for_tmp_sh_script w]} err_info] {
                log_info $info "Error to open file $file_for_tmp_sh_script. Reason: $err_info" 2
                return 0
        }
        log_info $info "content is $new_content" 2
        log_info $info "filename is $file_for_tmp_sh_script ; filehandle is $filehandle" 2
        _puts $filehandle "$new_content"
        if [catch {close $filehandle} err_info] {
                log_info $info "Error to close file. Reason: $err_info" 2
                return 0
        }
        if [catch { eval exec /bin/bash $file_for_tmp_sh_script > $file_for_tmp_sh_result} err_info] {
                log_info $info "Error to execute tclsh . Reason: $err_info" 2
                return 0
        }
        if [catch {set filehandle [open $file_for_tmp_sh_result]} err_info] {
                log_info $info "Error to open file $file_for_tmp_sh_result . Reason: $err_info" 2
                return 0
        }
        set result [read $filehandle]
        log_info $info "result is $result" 2
        log_info $info "filehandle is $filehandle" 2
        if [catch {close $filehandle} err_info] {
                log_info $info "Error to close file. Reason: $err_info" 2
                return 0
        }
	$run_result delete 1.0 end
        $run_result insert end $result
}
proc load_file {widget filename} {
	global info SCRIPT_DIR
	upvar $filename load_file_name
        set types {
                {{Tcl Files}  {.tcl} }
                {{Shell Files}  {.sh} }
                {{Expect Files}  {.exp} }
                {{Cmd Files}  {.cmd} }
                {{All Files}  *      }
        }
        set load_file_name [tk_getOpenFile -filetypes $types -initialdir $SCRIPT_DIR/data ]
        log_info $info "load file name: $load_file_name." 2
        if [catch {set filehandle [open $load_file_name]} err_info] {
                log_info $info "Error to open file $load_file_name . Reason: $err_info" 2
                return 0
        }
        set result [read $filehandle]
        log_info $info "result is $result" 2
        log_info $info "filehandle is $filehandle" 2
        if [catch {close $filehandle} err_info] {
                log_info $info "Error to close file. Reason: $err_info" 2
                return 0
        }
        $widget delete 1.0 end
        $widget insert end $result
}
proc save_file {widget type filename} {
	global info SCRIPT_DIR
	upvar $filename save_file_name
        set new_content [$widget get 1.0 end]
	if {$type == ".cmd"} {
	        set types {
        	        {{Cmd Files}  {.cmd} }
                	{{All Files}  *      }
	        }
	} else {
                set types {
                        {{Tcl Files}  {.tcl} }
                        {{Exp Files}  {.exp} }
                        {{All Files}  *      }
                }
	}
        set save_file_name [tk_getSaveFile -filetypes $types -initialdir $SCRIPT_DIR/data -initialfile $save_file_name]
	if {$save_file_name==""} {return 0}
        if [catch {set filehandle [open $save_file_name w]} err_info] {
                log_info $info "Error to open file $save_file_name. Reason: $err_info" 2
                return 0
        }
        log_info $info "content is $new_content" 2
        log_info $info "filename is $save_file_name ; filehandle is $filehandle" 2
        _puts $filehandle "$new_content"
        if [catch {close $filehandle} err_info] {
                log_info $info "Error to close file. Reason: $err_info" 2
                return 0
        }
}

